var zipList = {};
zipList.z_treegrid=$('#z_treegrid');// 主页面的 treegrid（DOM）
zipList.z_win=$("#win");//弹出窗口
zipList.z_form=$("#z_form");

$(function(){
    zipList.init();
    zipList.bindFunction();
    
});

zipList.init = function(){
	zipList.initTreegrid();
	zipList.initModal();//初始化窗口
}

zipList.initTreegrid = function(){
    zipList.z_treegrid.treegrid({    
        url:basePath+"/main/queryFirst",   
        idField:'id',
        rownumbers: true,
     //   pagination: true,
        fit:true,
        treeField:'name',
        toolbar: '#z_treegrid_tb',
        fitColumns:true,
        columns:[[    
            {title:'名称',field:'name',width:35,align:'left'},    
            {title:'备注',field:'remark',width:35,align:'center'} ,
            {title:'排序',field:'orderId',width:10,align:'center'} , 
            {title:'创建日期',field:'operTime',width:10,align:'center'} ,
            {title:'操作',field:'serverName',width:10,align:'center',
                  formatter : function(value, row, index) {
                            if(value !=null && value!=""){
                                var s="<a target=\"_blank\" href='"+"\\zip\\zip\\folder"+value;
                                   s+="'>";
                                   s+= "查看";
                                   s+="</a>";
                                  /* s+="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                                   s+="<a target=\"_blank\" href='"+"\\zip\\zip\\upload\\"+row.filename;
                                   s+="'>";
                                   s+= "下载";*/
                                   s+="</a>";
                                    return s;   
                            }
                       }
            }    
        ]],
        onBeforeExpand:function(row){
            zipList.z_treegrid.treegrid("options").url = basePath+"/main/queryNext?parentId="+row.id;  
            return true;      
        }  
    });  
};

/**
 *  easyui  按钮绑定事件
 */
zipList.bindFunction = function(){
    
    // 主页面按钮
    $("#z_add_btn").on("click",function(){
        zipList.add();
    });
    $("#z_update_btn").on("click",function(){
        zipList.update();
    });
    $("#z_delete_btn").on ("click",function(){
        zipList.remove();  
    });
    
};

zipList.initModal=function(){
    zipList.z_win.dialog({
        width:530,    
        height:430,    
        modal:true,
        draggable:false,//不可拖动
        shadow:true,//阴影
        collapsible:false,//不可折叠
        closed:true,
        onBeforeOpen:function(){
           zipList.z_form.form('reset');
           zipList.z_form.form('clear');
        },
        buttons:[{
            text:'确定',
            iconCls:'icon-ok',
            handler:function(){
                zipList.save();
            }
        },{
            text:'取消',
            iconCls:'icon-cancel',
            handler:function(){
                zipList.z_win.dialog('close');
                zipList.z_form.form('reset');
            }
        }]
    });  
};

//增加方法
zipList.add=function(){
    var selectNode=zipList.getSelected();
    if(selectNode){
    	if(selectNode.id==0){
            zipList.z_win.dialog('setTitle',"添加文件夹");
            zipList.z_win.dialog('open');
            zipList.hideLabel();
    	}else if(selectNode.modFlag==0){
    		 zipList.z_win.dialog('setTitle',"添加文件");
             zipList.z_win.dialog('open');
             $("#orderId").numberspinner("setValue","");
             //查询最大orderId
             $.ajax({
                url: base.path("/main/queryMaxOrderId"),
                type: "post",
                data:{"parentId":selectNode.id},
                async:false,
                success: function (data) {
                    if(data!=null){
                    	$("#orderId").numberspinner("setValue",data.orderId);
                    }
                }
             });
            zipList.showLabel();
    	}else{
    	  $.messager.alert('提示','<h4>请选择项目文件夹！</h4>');
    	}
    }else{
      $.messager.alert('提示','<h4>请选择项目文件夹或项目！</h4>');
    }
}

zipList.getSelected = function(){
    return  zipList.z_treegrid.treegrid('getSelected');
};

zipList.save=function(){
    var selectNode = zipList.getSelected();
    if(selectNode.id==0){//id=0新建文件夹
    	if(zipList.z_win.panel('options').title=='添加文件夹'){
    	   zipList.z_form.form('submit', {    
            url:basePath+"/main/insertZipFolder",     
            onSubmit: function(param){    
              if(!zipList.z_form.form('validate')){
                return false;  
              }   
              param.parentId=selectNode.id;
            },    
            success:function(data){
                if(data>0){
                    zipList.z_win.window('close');
                    $.messager.alert('提示','<h4>操作成功！</h4>');
                    zipList.initTreegrid();
                    return;
                }else{
                    $.messager.alert('提示','<h4>操作失败！</h4>');
                    return;
                }
            }    
            }); 
    	}
    }else{
    	if(zipList.z_win.panel('options').title=='添加文件'){
            zipList.z_form.form('submit', {    
            url:basePath+"/main/insertProject",     
            onSubmit: function(param){    
              if(!zipList.z_form.form('validate')){
                return false;  
              }   
              param.parentId=selectNode.id;
              var path=$("#pathHid").val();
              var fileName=$("#fileNameHid").val();
              if(path!=null && path !="" && fileName!=null && fileName!=""){
              }else{
                  $.messager.alert('提示','<h4>请上传压缩包文件！</h4>');
                  return false;
              }
            },    
            success:function(data){
                if(data>0){
                    $.messager.alert('提示','<h4>操作成功！</h4>');
                    zipList.z_win.window('close');
                     zipList.initTreegrid();
                    return;
                }else{
                    $.messager.alert('提示','<h4>操作失败！</h4>');
                    return;
                }
            }    
            }); 
        }
        if(zipList.z_win.panel('options').title=='修改文件夹'){
            zipList.z_form.form('submit', {    
            url:basePath+"/main/updateZipFolder",     
            onSubmit: function(param){    
              if(!zipList.z_form.form('validate')){
                return false;  
              }   
              param.id=selectNode.id;
            },    
            success:function(data){
                if(data>0){
                    $.messager.alert('提示','<h4>操作成功！</h4>');
                    zipList.z_win.window('close');
                     zipList.initTreegrid();
                    return;
                }else{
                    $.messager.alert('提示','<h4>操作失败！</h4>');
                    return;
                }
            }    
            }); 
        }
        if(zipList.z_win.panel('options').title=='修改文件备注'){
           zipList.z_form.form('submit', {    
            url:basePath+"/main/updateProject",     
            onSubmit: function(param){    
              if(!zipList.z_form.form('validate')){
                return false;  
              }   
              param.id=selectNode.id;
            },    
            success:function(data){
                if(data>0){
                    $.messager.alert('提示','<h4>操作成功！</h4>');
                    zipList.z_win.window('close');
                     zipList.initTreegrid();
                    return;
                }else{
                    $.messager.alert('提示','<h4>操作失败！</h4>');
                    return;
                }
            }    
            }); 
        }
    }
    
};

zipList.update=function(){
    var selectNode=zipList.getSelected();
    if(selectNode){
    	if(selectNode.id==0){
    		 $.messager.alert('提示','<h4>请选择项目文件夹或项目！</h4>');
    	}else{
           if(selectNode.modFlag==0){
            zipList.z_win.window('setTitle',"修改文件夹");
            zipList.z_win.dialog('open');
            zipList.z_form.form('load',selectNode);
            zipList.hideLabel();
            }else{
                zipList.z_win.window('setTitle',"修改文件备注");
                zipList.z_win.dialog('open');
                zipList.z_form.form('load',selectNode);
                zipList.showLabel();
                $("#fileLabel").hide();
                $('#fileLabel').attr("disabled",true);
            }    	
    	}
    }else{
      $.messager.alert('提示','<h4>请选择项目文件夹或项目！</h4>');
    }
}

zipList.hideLabel=function(){
    $("#remarkLabel").hide();
    $('#remarkLabel').attr("disabled",true);
    $("#fileLabel").hide();
    $('#fileLabel').attr("disabled",true);
}

zipList.showLabel=function(){
    $("#remarkLabel").show();
    $("#fileLabel").show(); 
    $('#fileLabel').attr("disabled","");
    $('#fileLabel').attr("disabled","");
    $("#file_tableTbody").html("");   
    $("#fileWin_upload").attr("onclick","uploadFile()");
}

zipList.remove=function(){
   var selectNode = zipList.getSelected();
    if(selectNode && selectNode.modFlag==1){
        $.messager.confirm('确认','<h4>您确认想要删除['+selectNode.name+']吗？</h4>',function(r){    
            if (r){    
                 $.ajax({
                    url: base.path("/main/deleteProject"),
                    type: "post",
                    data:{"id":selectNode.id},
                    async:false,
                    success: function (data) {
                        if(data == 1){
                            $.messager.alert('提示','<h4>删除成功！</h4>');
                              zipList.initTreegrid();
                            return;
                        }else{
                            $.messager.alert('提示','<h4>删除失败，网络异常！</h4>');
                            return;
                        }
                    }
                });
            }    
        });  
        
    }else{
        $.messager.alert('提示','<h4>请选择一个项目！</h4>');
        return;
    }
}

function uploadFile(){
    var options = {
                fileSizeLimit: 5 * 1024 * 1024, //5M
                fileNumLimit:1,// 单次最多上传文件1个
                all_call_back :function(reponseList){
                    //所有文件集合， 是个array
                    $("#pathHid").val(reponseList[0].path);
                    $("#fileNameHid").val(reponseList[0].fileName);
                    var fileName=reponseList[0].fileName.substring(0,reponseList[0].fileName.length-4)
                    var html="";
                        html+="<li class=\"lg_upload_ul_li\">";
                        html+="<span>";
                        html+=fileName;
                        html+="</span>";
                        html+="</li>";
                    $("#name").textbox('setValue',fileName);    
                    $("#file_tableTbody").append(html);   
                    $("#fileWin_upload").attr("onclick","");
                }
        };
        base.openUpload(options);
}